package com.cungmua.app.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.cungmua.app.R;
import com.cungmua.app.base.BaseAuthenticatedActivity;
import com.cungmua.app.handler.ChangePassHandler;
import com.cungmua.app.model.Cart;
import com.cungmua.app.model.request.ChangePassRequest;
import com.cungmua.app.model.request.LoginRequest;
import com.cungmua.app.model.response.ChangePassResponse;
import com.cungmua.app.task.ChangePassTask;
import com.cungmua.app.utils.AppUtils;

/**
 * Created by kelvin.nguyen on 7/2/14.
 */
public class PasswordChangeActivity extends BaseAuthenticatedActivity implements ChangePassHandler {

    private EditText txtOldPass;
    private EditText txtPassword;
    private EditText txtPasswordConfirmation;
    private Button btnUpdate;
    private ImageView icon_home;
    private Button icon_cart;
    private LinearLayout btnCart;

    @Override
    public int getLayoutResource() {
        return R.layout.activity_password_change;
    }

    @Override
    public void updateView() {

    }

    @Override
    public void loadControls(Bundle savedInstanceState) {
        if (getCurrentUser().getId() == 0) {
            setResult(RESULT_CANCELED);
            finish();
            return;
        }
        txtOldPass = (EditText) findViewById(R.id.txtOldPass);
        txtPassword = (EditText) findViewById(R.id.txtPassword);
        txtPasswordConfirmation = (EditText) findViewById(R.id.txtPasswordConfirmation);
        btnUpdate = (Button) findViewById(R.id.btnUpdate);

        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!validateFields())
                    return;

                PasswordChangeActivity ac = PasswordChangeActivity.this;
                ChangePassRequest request = new ChangePassRequest(ac.getDeviceId(), ac.getToken());
                request.setUserId(ac.getCurrentUser().getId());
                request.setNewPass(AppUtils.MD5(txtPassword.getText().toString()));
                request.setOldPass(AppUtils.MD5(txtOldPass.getText().toString()));

                ChangePassTask task = new ChangePassTask(ac, ac);
                task.execute(request);
            }
        });

        icon_home=(ImageView)findViewById(R.id.icon_home);
        icon_cart=(Button)findViewById(R.id.icon_cart);
        btnCart=(LinearLayout)findViewById(R.id.btnCart);


        Cart currentCart=getCurrentCart();
        icon_cart.setText(currentCart.getTotalQuantity()+"");

        icon_home.setOnClickListener(new ImageView.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMenu();
            }
        });

        btnCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToYourCart();
            }
        });

    }

    public void alert(String alert) {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage(alert)
                .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .show();
    }

    private boolean validateFields() {
        if (txtOldPass.getText().toString().isEmpty()) {
            alert(getString(R.string.must_have_old_password));
            return false;
        }
        if (txtPassword.getText().toString().isEmpty()) {
            alert(getString(R.string.must_have_password));
            return false;
        }
        if (txtPassword.getText().length() < 6) {
            alert(getString(R.string.password_min_length, 6));
            return false;
        }
        if (!txtPassword.getText().toString().equals(txtPasswordConfirmation.getText().toString())) {
            alert(getString(R.string.wrong_password_confirmation));
            return false;
        }
        return true;
    }

    @Override
    public void onPasswordChange(ChangePassResponse response) {
        if (response.getCode() == 0) {
            Toast.makeText(this, R.string.password_changed, Toast.LENGTH_SHORT).show();
            LoginRequest lastLoginRequest = getLastLoginRequest();
            lastLoginRequest.setPassword(response.getNewPass());

            setLastLoginRequest(lastLoginRequest);

            setResult(RESULT_OK);
            finish();
        } else {
            alert(response.getMessage());
        }
    }
}
